package com.gtl.meishi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.gtl.meishi.R;
import com.gtl.meishi.adapter.GridViewAdapter;
import com.gtl.meishi.bean.CotegroyEntity;
import com.gtl.meishi.bean.MediaEntity;
import com.gtl.meishi.model.MediaModel;
import com.gtl.meishi.ui.DetailActivity;
import com.gtl.meishi.util.HttpURLUtil;

import java.util.ArrayList;
import java.util.List;

public class HotFragment extends Fragment {

    private CotegroyEntity cotegroyEntity;

    public CotegroyEntity getCotegroyEntity() {
        return cotegroyEntity;
    }

    public HotFragment setCotegroyEntity(CotegroyEntity cotegroyEntity) {
        this.cotegroyEntity = cotegroyEntity;
        return this;
    }

    private ListView mListView;
    private GridView mGridView;
    private GridViewAdapter adapter;
    private List<MediaEntity> entityList = new ArrayList<>();

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    List<MediaEntity> list = (List<MediaEntity>) msg.obj;
                    entityList.addAll(list);
                    if (page == 1) {
                        adapter = new GridViewAdapter(entityList, getActivity());
                        mGridView.setAdapter(adapter);
                    } else {
                        entityList.addAll(list);
                        adapter.notifyDataSetChanged();
                    }

                    break;

            }
        }
    };

    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_hot_fragment, container, false);
        //mListView= (ListView) v.findViewById(R.id.listview_hot_fragment);
        mGridView = (GridView) v.findViewById(R.id.gridview_hot_fragment);
        String URL = getURL(getCotegroyEntity());

        MediaModel.getList(handler, URL, getContext(), page, getCotegroyEntity());
        setListen();
        return v;
    }

    public String getURL(CotegroyEntity cotegroyEntity) {
        String url = HttpURLUtil.BASE_URL;
        switch (cotegroyEntity.getType()) {

            case 1:
                url += HttpURLUtil.HOT_VIDEO_LIST_URL;
                break;
            default:
                url += HttpURLUtil.VIDEO_LIST + "";
                break;

        }

        return url;


    }


    public void setListen() {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                MediaEntity mediaEntity = (MediaEntity) mGridView.getItemAtPosition(i);
                MediaEntity.MediaBean media = mediaEntity.getMedia();
                if (media != null) {
                    intent.putExtra("video", media.getVideo());
                    intent.putExtra("likes_count", media.getLikes_count());
                    intent.putExtra("caption", media.getCaption());
                    startActivity(intent);

                } else {
                    Toast.makeText(getActivity(), "非视频资源", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}
